#!/bin/bash
broken_files=()
broken_lines=()
broken_lines_numbers=()
if [[ -z "${SRC_DIRECTORY}" ]]; then
    echo "Variable SRC_DIRECTORY was not set"
    exit 1
fi

cd "${SRC_DIRECTORY}"

IFS=$'\n'
for file in $(find . -type f \( -name "*.h" -o -name "*.cpp" \)); do
    for line in $(grep -n "#include.*\.cpp" "$file"); do
        broken_files+=("$file")
        broken_lines+=($(echo "$line" | cut -f2 -d:))
        broken_lines_numbers+=($(echo "$line" | cut -f1 -d:))
    done
done

if [ ${#broken_files[*]} == 0 ]; then
    echo "All files do not contain '#include \"*.cpp\""
    exit 0
fi

printf "Files that use '#include \"*.cpp\" have been detected\n"
printf "Number of detected lines: %4d\n" "${#broken_files[*]}"
printf "Please delete the '#include \"*.cpp\" from the files listed below:\n"

for index in ${!broken_files[*]}; do
    printf "%s in line %s: %s\n" "${broken_files[$index]}" "${broken_lines_numbers[$index]}" "${broken_lines[$index]}"
done
exit 1
